/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.lcfgly.Service;

import com.lcfgly.Dao.PriceImpl;
import com.lcfgly.Dao.Product;
import com.lcfgly.Database.ShopPrices;
import com.lcfgly.Exception.ActionException;
import com.lcfgly.Exception.ErrorException;
import com.lcfgly.Exception.SQLEmptyException;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author lcfgly
 */
@Service
public class PriceService implements Serializable{
    
    Logger log=Logger.getLogger("SqlError");
    
    @Autowired
    PriceImpl pricesImpl;
    
    @Autowired
    Product product;
    
    /**
     * 保存价格
     * @param prices
     * @param userid 用户ID
     * @return
     * @throws com.lcfgly.Exception.ActionException
     */
    public boolean save(ShopPrices prices,int userid) throws  ActionException
    {
        if(prices == null)
            throw new ActionException("空数据 !");
        else if(prices.getProduct() == 0)
            throw new ActionException("数据错误！未指定产品");
        else if(prices.getShopid() == 0)
            throw new ActionException("数据错误！未指定店铺");
        else if(prices.getId()==null){
            try {
                ShopPrices result=find(userid, prices.getProduct());
                result.setPrice(prices.getPrice());
                int x=pricesImpl.update(prices, userid);
                return x==1;
            } catch (SQLException ex) {
                log.error(ex);
                throw new ErrorException();
            }
        }else{
            try{
                int result=pricesImpl.update(prices,userid);
                if(result == 1)
                    return true;
                else
                    throw new SQLException("数据修改失败！");
            }catch(SQLException e){
                log.error(e);
                throw new ErrorException();
            }
        }
    }
    
    /**
     * 创建一个价格
     * @param prices
     * @param userid
     * @return
     * @throws com.lcfgly.Exception.ActionException
     */
    public boolean create(ShopPrices prices,int userid) throws ActionException
    {
        if(prices == null)
            throw new ActionException("空数据 !");
        else if(prices.getProduct() == 0)
            throw new ActionException("数据错误！未指定产品");
        else if(prices.getShopid() == 0)
            throw new ActionException("数据错误！未指定店铺");
        else{
            try{
                ShopPrices result=pricesImpl.findbyShopidAndProduct(userid, prices.getProduct());
                result.setPrice(prices.getPrice());
                result.setPriceimg(prices.getPriceimg());
                return save(result,userid);
            }catch(SQLEmptyException e){
                log.error(e);
                try {
                    prices.setShopid(userid);
                    int result=pricesImpl.insert(prices);
                    if(result == 1)
                        return true;
                    else
                        throw new SQLException("操作失败！");
                } catch (SQLException ex) {
                    log.error(ex);
                    throw new ErrorException();
                }
            }catch(SQLException e){
                log.error(e);
                throw new ErrorException();
            }
        }
    }
    
    /**
     * 删除
     * @param prices
     * @param userid
     * @return
     * @throws com.lcfgly.Exception.ActionException
     */
    public boolean delete(ShopPrices prices,int userid) throws ActionException
    {
        if(prices == null)
            throw new ActionException("空数据 !");
        else if(prices.getId() == null)
        {
            try {
                ShopPrices result=find(userid, prices.getProduct());
                pricesImpl.delete(result, userid);
                return true;
            } catch (SQLException ex) {
                throw new ActionException(ex.getMessage());
            }
        }else{
            try{
                pricesImpl.deletebyId(prices.getId(),userid);
                return true;
            }catch(SQLException e){
                log.error(e);
                throw new ErrorException();
            }
        }
    }
    
    /**
     * 查找
     * @param shopid
     * @param product
     * @return
     * @throws com.lcfgly.Exception.ActionException
     */
    public ShopPrices find(int shopid,int product) throws ActionException
    {
        try{
            ShopPrices prices=pricesImpl.findbyShopidAndProduct(shopid, product);
            return prices;
        }catch(SQLEmptyException e){
            throw new ActionException("未查找到先关数据！",e);
        }catch(SQLException e){
            log.error(e);
            throw new ErrorException();
        }
    }
    
    /**
     * 获取首页产品列表
     * @param shopid
     * @return
     * @throws ActionException 
     */
    public List indexProductList(int shopid)throws ActionException{
        List price;
        try{
            price=pricesImpl.indexProductList(shopid);
        }catch(SQLEmptyException e){
            log.info(e);
            return null;
        }catch(SQLException e){
            log.error(e);
            return null;
        }
        List resuList=new ArrayList();
        for (Object object : price) {
            Map res=new HashMap();
            Map m=(Map)object;
            int productid=Integer.valueOf(m.get("product").toString());
            int type=Integer.valueOf(m.get("producttype").toString());
            try{
                res.put("price", m);
                res.put("product", product.GetProductInfo(type, productid));
            }catch(SQLException e){
                log.error(e);
                continue;
            }
            resuList.add(res);
        }
        return resuList;
    }
    
    /**
     * 产品列表
     * @param shopid
     * @param page
     * @return 
     */
    public Map productlist(int shopid,int page){
        try{
            Map price=pricesImpl.findbyShopid(shopid, page);
            List resuList = new ArrayList();
            if(price.get("data") != null){
                List data=(List)price.get("data");
                for (Object object : data) {
                    Map res = new HashMap();
                    ShopPrices m = (ShopPrices) object;
                    int productid = Integer.valueOf(m.getProduct());
                    int type = Integer.valueOf(m.getProducttype());
                    try {
                        res.put("price", m);
                        res.put("product", product.GetProductInfo(type, productid));
                    } catch (SQLException e) {
                        log.error(e);
                        continue;
                    }
                    resuList.add(res);
                }
            }
            price.put("data", resuList);
            return price;
        }catch(SQLException e){
            log.error(e);
            return null;
        }
    }
    
    /**
     * 分类产品列表
     * @param shopid
     * @param typeid
     * @param page
     * @return 
     */
    public Map productlist(int shopid,int typeid,int page){
        try{
            Map price=pricesImpl.findProductByShopidAndTypeid(shopid, typeid, page);
            List resuList = new ArrayList();
            if(price.get("data") != null){
                List data=(List)price.get("data");
                for (Object object : data) {
                    Map res = new HashMap();
                    ShopPrices m = (ShopPrices) object;
                    int productid = Integer.valueOf(m.getProduct());
                    int type = Integer.valueOf(m.getProducttype());
                    try {
                        res.put("price", m);
                        res.put("product", product.GetProductInfo(type, productid));
                    } catch (SQLException e) {
                        log.error(e);
                        continue;
                    }
                    resuList.add(res);
                }
            }
            price.put("data", resuList);
            return price;
        }catch(SQLException e){
            log.error(e);
            return null;
        }
    }
    
    /**
     * 获取已报价产品id字串
     * @param shopid
     * @param typeid
     * @return 
     */
    public List downproduct(int shopid,int typeid){
        List<Map> res=null;
        try{
            res=pricesImpl.downProductList(shopid, typeid);
        }catch(SQLException e){
            log.error(e);
        }
        return res;
    }
    
}
